package my.jieh.tips.util.collection;

import java.util.BitSet;

public class BitSetTest1 {
  public static void main(String args[]) {
    // create a BitSet

    BitSet bitset = new BitSet();

    // set the 0th and 1000th bits

    bitset.set(0);
    bitset.set(1000);

    // display the size, length,
    // and cardinality

    System.out.println("size = " + bitset.size());
    System.out.println("length = " + bitset.length());
    System.out.println("cardinality = " + bitset.cardinality());

    // list the bits that are set,
    // using toString()

    System.out.println("list #1 = " + bitset);

    // list the bits that are set,
    // using a loop

    System.out.print("list #2 = ");
    for (int i = 0, len = bitset.length(); i < len; i++) {
      if (bitset.get(i)) {
        System.out.print(i + " ");
      }
    }
    System.out.println();

    // list the bits that are set,
    // using an iterator

    System.out.print("list #3 = ");
    for (int i = bitset.nextSetBit(0); i >= 0;
                 i = bitset.nextSetBit(i + 1)) {
      System.out.print(i + " ");
    }
    System.out.println();

  } // end class
}